﻿using Aspose.Hook;
using Aspose.Words;
using Aspose.Words.Reporting;
using NUnit.Framework;

namespace WordStudy20250123.T001
{
    [TestFixture]
    public class WordTest01
    {
        [SetUp]
        public void Setup()
        {
            HookManager.ShowHookDetails(true);
            HookManager.StartHook();
        }

        [Test]
        public void Test01()
        {
            string outputPath = @"D:\data\WordStudy20250123\WordTest01\WordTest01_Test01.docx";
            CreateReport2(outputPath);
        }

        public static void CreateReport2(string outputPath)
        {
            string fileName = @"D:\data\WordStudy20250123\WordTest01\simpleTemplate01.docx";
            // Load the template document.
            Document doc = new Document(fileName);

            // Create an instance of sender class to set it's properties.
            Sender s = new Sender { Name = "LINQ Reporting Engine", Message = "Hello World" };

            // Create a Reporting Engine.
            ReportingEngine engine = new ReportingEngine();

            // Execute the build report.
            engine.BuildReport(doc, s, "s");

            // Save the finished document to disk.
            doc.Save(outputPath);
        }

        [Test]
        public void Test02()
        {
            string templatePath = @"D:\data\WordStudy20250123\WordTest01\simpleTemplate02.docx";
            string outputPath = @"D:\data\WordStudy20250123\WordTest01\WordTest01_Test02.docx";
            GenerateReport(templatePath, outputPath);
        }

        public static void GenerateReport(string templatePath, string outputPath)
        {
            try
            {
                // 加载模板文档
                Document doc = new Document(templatePath);

                // 准备数据
                var data = new ReportData
                {
                    Title = "学生成绩报告",
                    Date = DateTime.Now.ToString("yyyy-MM-dd"),
                    PhotoPath = @"D:\data\WordStudy20250123\WordTest01\dingliren.png",
                    IsPass = true,
                    Students = new List<Student>
                    {
                        new Student
                        {
                            Name = "zhangsan",
                            IsQualified = true,
                            Score = 85,
                        },
                        new Student
                        {
                            Name = "lisi",
                            IsQualified = false,
                            Score = 59,
                        },
                        new Student
                        {
                            Name = "wangwu",
                            IsQualified = true,
                            Score = 92,
                        },
                    },
                };

                var data1 = new Dictionary<string, string>()
                {
                    { "Date", DateTime.Now.ToString("yyyy-MM-dd") },
                    { "PhotoPath", @"dingliren.png" },
                    { "IsPass", "true" },
                    { "Title", "zhangsan,lisi,wangwu" },
                };
                // 创建引擎并配置
                var engine = new ReportingEngine();

                // 使用简单的数据源名称
                engine.BuildReport(doc, data, "s");

                // 保存文档
                doc.Save(outputPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

    public class Sender
    {
        public string Name { get; set; }
        public string Message { get; set; }
    }

    public class Student
    {
        public string Name { get; set; }
        public bool IsQualified { get; set; }
        public double Score { get; set; }
    }

    public class Course
    {
        public string CourseName { get; set; }
        public double Credit { get; set; }
    }

    public class ReportData
    {
        public string Title { get; set; }
        public string Date { get; set; }
        public string PhotoPath { get; set; }
        public bool IsPass { get; set; }
        public List<Student> Students { get; set; }
        public List<Course> Courses { get; set; }
    }
}
